<?php

/**
 * 下单
 * /api/order.php?issue=3418889&treeid=2&pid=4&money=550
 * 
 */
require 'database.php';

$issue      =$_GET["issue"];
$tree_id    =(int)$_GET["treeid"];
$pid        =(int)$_GET["pid"];
$money      =(int)$_GET["money"];
$poin       =(int)$_GET["poin"];
$___id      =(int)$_GET["id"];

$copyorder=file_get_contents("copyorder.json");
$copyorder=json_decode($copyorder,1);

$_____order_____=file_get_contents("order.json");
$_____order_____=json_decode($_____order_____,1);
$_____issue_____=file_get_contents("issue.json");
$_____issue_____=json_decode($_____issue_____,1);

if(!$_____issue_____[0]){
    $_____data_____=[
        [
            "id"=>1,
            "issue"=>$issue
        ]
    ];
    $_____issue_____=$_____data_____;
}
if($_____issue_____){
    $p=0;    
    for ($i = 0; $i < count($_____issue_____); $i++) {
        if($_____issue_____[$i]["issue"]==$issue){
            $p++;
        }
    }
    if($p==0){
        $count=count($_____issue_____);
        $_____issue_____[$count]["id"]=$_____issue_____[$count-1]["id"]+1;
        $_____issue_____[$count]["issue"]=$issue;
    }
}

$___poin____=0;
// for ($i = 0; $i < count($copyorder); $i++) {
//     if($copyorder[$i]["tree_id"]==$tree_id && $copyorder[$i]["pid"]==$pid && $copyorder[$i]["issue"]==$_____issue_____[count($_____issue_____)-1]["issue"] && $copyorder[$i]["id"]==$___id){
//         $___poin____=$copyorder[$i]["poin"];
//     }
// }

if(!empty($_____order_____)){
    if($_____order_____[count($_____order_____)-1]["issue"]!=$issue){
        // $copyorder=file_get_contents("copyorder.json");
        // if(!empty($copyorder)){
        //     $copyorder=json_decode($copyorder,1);
        //     for ($i = 0; $i < count($_____order_____); $i++) {
        //         array_push($copyorder,$_____order_____[$i]);
        //     }
        // }else{
        //     $copyorder=$_____order_____;
        // }
      
        
        // $copyorder=json_encode($copyorder,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
        // file_put_contents("copyorder.json",$copyorder);
        $_____order_____=[];
    }
    $_count_=count($_____order_____);
    $_____order_____[$_count_]["id"]        =$_____order_____[$_count_-1]["id"]+1;
    $_____order_____[$_count_]["tree_id"]   =$tree_id;
    $_____order_____[$_count_]["pid"]       =$pid;
    $_____order_____[$_count_]["issue"]     =$issue;
    $_____order_____[$_count_]["money"]     =$money;
    $_____order_____[$_count_]["order_no"]  ="";
    $_____order_____[$_count_]["poin"]      =$poin==0 ? 0:$poin;
    $_____order_____[$_count_]["result"]    ="";
}

$_____issue=json_encode($_____issue_____,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
$_____order_____=json_encode($_____order_____,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);

$___is_issue___=file_get_contents("issue.json");
$___is_issue___=json_decode($___is_issue___,1);
$___issue___=0;
if(!empty($___is_issue___)){
    $___is_issue______=$___is_issue___[count($___is_issue___)-1]["issue"];
    if($___is_issue______==$issue){
        $___issue___=1; //查询issue表已存在当前值
    }
}
$_i__=file_put_contents("issue.json",$_____issue);



$__o___=file_put_contents("order.json",$_____order_____);

if($___issue___==0){
    $__F=file_get_contents("http://".$_SERVER["HTTP_HOST"]."/api/auto.php");
    $__F=json_decode($__F,1);
    
    if($__F["code"]!=200){
        return json([
            "code"=>-1,
            "msg"=>"reutrn response fuiled!!!"
        ]);
    }
}
if(!$_i__){
    return json([
        "code"=>200,
        "msg"=>"issue fuild"
    ]);
}
if(!$__o___){
    return json([
        "code"=>-1,
        "msg"=>"order fuiled"
    ]);
}
return json([
    "code"=>200,
    "msg"=>"successful"
]);

